package com.jz.yanshe.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fynn.fluidlayout.FluidLayout;
import com.hyphenate.chat.ChatClient;
import com.hyphenate.helpdesk.callback.Callback;
import com.hyphenate.helpdesk.easeui.util.IntentBuilder;
import com.hyphenate.helpdesk.model.ContentFactory;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.GoodsTabResponse;
import com.jz.jd.rest.response.KeFuResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.jz.yanshe.base.BaseFragment;
import com.jz.yanshe.ui.SearchListActivity;
import com.jz.yanshe.ui.widget.CheckBoxText;
import com.jz.yanshe.ui.widget.CustomPopWindow;
import com.jz.yanshe.ui.SearchActivity;
import com.jz.yanshe.R;
import com.jz.yanshe.util.PreferencesHelper;

/**
 * Created by Administrator on 2018/2/26.
 */

public class MallFragment extends BaseFragment {

    private static MallFragment mallFragment;


    @BindView(R.id.timeline_tablayout)
    TabLayout mTabLayout;
    @BindView(R.id.timeline_viewpager)
    ViewPager mViewPager;
    //    @BindView(R.id.banner)
//    Banner banner;
    @BindView(R.id.btn_select)
    TextView btn_select;

    @BindView(R.id.btn_type)
    ImageView btn_type;

    private String user_name;
    private String password;
    List<GoodsTabResponse.GoodsTab> goodsTabList = new ArrayList<>();
    private CustomPopWindow mListPopWindow;

    List<String> list = new ArrayList<>();
    ArrayList<CheckBoxText> listType = new ArrayList<>();
    ArrayList<CheckBoxText> listOrder = new ArrayList<>();

    public static MallFragment newInstance(Bundle bundle) {
        if (mallFragment == null) {
            mallFragment = new MallFragment();
            mallFragment.setArguments(bundle);
            return mallFragment;
        } else {
            return mallFragment;
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_mall;
    }

    @Override
    protected void initView() {
        mViewPager.setAdapter(new MallPageAdapter(getActivity(), getChildFragmentManager()));
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mViewPager.setCurrentItem(0);
        mTabLayout.getTabAt(0).select();
    }

    @Override
    protected void initData() {
//        getBanner();
        goodsTabList = (List<GoodsTabResponse.GoodsTab>) getActivity().getIntent().getSerializableExtra("list");
        getKeFu(new Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }


    class MallPageAdapter extends FragmentPagerAdapter {
        private final int PAGE_COUNT = goodsTabList.size();

        private Context mContext;

        public MallPageAdapter(Context context, FragmentManager fm) {
            super(fm);
            this.mContext = context;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment f = null;
            if (!TextUtils.isEmpty(goodsTabList.get(position).goods_type_num) && !goodsTabList.get(position).goods_type_num.equals("6")) {
                f = HotFragment.newInstance(goodsTabList.get(position).goods_type_num,goodsTabList.get(position).goods_type_id);
            }else{
                f = WebViewFragment.newInstance(goodsTabList.get(position).goods_type_url);
            }
//            switch (position) {
//                case 0:
//                    f = HotFragment.newInstance(goodsTabList.get(position).goods_type_num);
//                    break;
//                case 1:
//                    f = HotFragment.newInstance(goodsTabList.get(position).goods_type_num);
//                    break;
//                case 2:
//                    f = HotFragment.newInstance(goodsTabList.get(position).goods_type_num);
//                    break;
//                case 3:
//                    f = HotFragment.newInstance(goodsTabList.get(position).goods_type_num);
//                    break;
//                case 4:
//                    f = HotFragment.newInstance(goodsTabList.get(position).goods_type_num);
//                    break;
//                case 5:
//                    type = 5;
//                    break;
//                case 6:
//                    type = 6;
//                    break;
//                case 7:
//                    type = 7;
//                    break;
//                default:
//                    f = HotFragment.newInstance("4");
//                    break;
//            }
            return f;
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return goodsTabList.get(position).goods_type_name;
//            switch (position) {
//                case 0:
//                    return "推荐";
//                case 1:
//                    return "新品";
//                case 2:
//                    return "女装";
//                case 3:
//                    return "男装";
//                case 4:
//                    return "童装";
//                case 5:
//                    return "已完成";
//                case 6:
//                    return "已取消";
//                case 7:
//                    return "待退货确认";
//                default:
//                    return "热销榜";
//            }
        }
    }


    @OnClick(R.id.btn_search)
    void btn_search() {
        startActivity(SearchActivity.class);
    }


    @OnClick(R.id.btn_kefu)
    void btn_kefu() {
        if (ChatClient.getInstance().isLoggedInBefore()) {
            Intent intent = new IntentBuilder(getActivity())
                    .setServiceIMNumber("kefuchannelimid_338790") //获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“IM服务号”
                    .build();
            startActivity(intent);
        } else {
            getKeFu(new Callback() {
                @Override
                public void onSuccess() {
                    Intent intent = new IntentBuilder(getActivity())
                            .setServiceIMNumber("kefuchannelimid_338790") //获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“IM服务号”
                            .setScheduleAgent(ContentFactory.createAgentIdentityInfo("2069427461@qq.com"))
                            .build();
                    startActivity(intent);
                }

                @Override
                public void onError(int i, String s) {

                }

                @Override
                public void onProgress(int i, String s) {

                }
            });
        }
    }

    private void getKeFu(final Callback callback) {
        RestProxy.getInstance().getKeFu(PreferencesHelper.getInstance().getString(getActivity(), "token"), new Observer<KeFuResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(KeFuResponse keFuResponse) {
                ChatClient.getInstance().login(keFuResponse.map.username, keFuResponse.map.password, callback);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @OnClick(R.id.btn_type)
    void btn_type() {
        showPopListView();
    }

    @SuppressLint("WrongViewCast")
    private void showPopListView() {
        listType.clear();
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.popwindow_mall, null);

        FluidLayout container_mall = contentView.findViewById(R.id.container_mall);
        for (int i = 0; i < goodsTabList.size(); i++) {
            final int j = i;
            View view = getLayoutInflater().inflate(R.layout.item_tab, null);
            final TextView tv_name = view.findViewById(R.id.tv_name);
            tv_name.setText(goodsTabList.get(i).goods_type_name);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPager.setCurrentItem(j);
                    mTabLayout.getTabAt(j).select();
                    mListPopWindow.dissmiss();
                }
            });
            container_mall.addView(view);
        }

//        CheckBoxText checkBoxText1 = contentView.findViewById(R.id.textView1);
//        CheckBoxText checkBoxText2 = contentView.findViewById(R.id.textView2);
//        CheckBoxText checkBoxText3 = contentView.findViewById(R.id.textView3);
//        CheckBoxText checkBoxText4 = contentView.findViewById(R.id.textView4);
//        CheckBoxText checkBoxText5 = contentView.findViewById(R.id.textView5);
//        checkBoxText1.type = 0;
//        checkBoxText2.type = 1;
//        checkBoxText3.type = 2;
//        checkBoxText4.type = 3;
//        checkBoxText5.type = 4;
//
//        listType.add(checkBoxText1);
//        listType.add(checkBoxText2);
//        listType.add(checkBoxText3);
//        listType.add(checkBoxText4);
//        listType.add(checkBoxText5);
//
//        CheckBoxText.OnGroup onGroup1 = new CheckBoxText.OnGroup() {
//            @Override
//            public void onGroup() {
//                for (CheckBoxText cbt : listType) {
//                    cbt.isCheck = false;
//                    cbt.clear();
//                }
//            }
//
//            @Override
//            public void startDate() {
//                int descOrAsc = 0;
//                int sortByType = 0;
//                int goodsCategoryId = 0;
//                for (CheckBoxText cbt : listType) {
//                    if (cbt.isCheck) {
//                        goodsCategoryId = cbt.goodsCategoryId;
//                        mViewPager.setCurrentItem(cbt.type);
//                        mTabLayout.getTabAt(cbt.type).select();
//                    }
//                }
////                getHotGoods(page, 10, descOrAsc, sortByType, goodsCategoryId);
////                EventBus.getDefault().post(new ChooseHot(descOrAsc, sortByType, goodsCategoryId));
//                mListPopWindow.dissmiss();
//            }
//        };
//        checkBoxText1.onGroup = onGroup1;
//        checkBoxText2.onGroup = onGroup1;
//        checkBoxText3.onGroup = onGroup1;
//        checkBoxText4.onGroup = onGroup1;
//        checkBoxText5.onGroup = onGroup1;


        //处理popWindow 显示内容
        //创建并显示popWindow
        mListPopWindow = new CustomPopWindow.PopupWindowBuilder(getActivity())
                .setView(contentView)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) // 显示大小
                .create()
                .showAsDropDown(btn_type, 0, 0);
    }

}
